package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.param.template.TemplateCompanyParam;
import cn.ulc.socket.admin.model.param.template.TemplateContentParam;
import cn.ulc.socket.admin.model.param.template.TemplateSaveParam;
import cn.ulc.socket.admin.service.template.TemplateServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

@RestController
@RequestMapping(value = "/template")
public class TemplateCtrl {
    private static final Log log = LogFactory.get();

    @Autowired
    TemplateServ templateServ;


    @GetMapping("/companyInfo")
    public Result findCompanyInfo(){
        return Result.success(templateServ.findCompanyInfo());
    }

    @PostMapping("/saveCompany")
    public Result saveCompany(@Validated @RequestBody TemplateCompanyParam param){
        return Result.success(templateServ.saveCompany(param));
    }


    @GetMapping("/content/{id}")
    public Result findDetail(@PathVariable("id") Long id){
        return Result.success(templateServ.findContent(id));
    }

    /**
     * 保存模板内容
     * @param param
     * @return
     */
    @PostMapping("/saveContent")
    public Result saveTemplateContent(@Validated @RequestBody TemplateContentParam param){
        //resultId 为templateid >0 表示更新操作成功
        Long resultId = templateServ.saveContent(param);
        return Result.success(resultId);
    }

    @GetMapping
    public Result findList(String title,  String status, PaginationParam param){
        return Result.success(templateServ.findListPage(title,status,param));
    }

    @PostMapping
    public Result saveOne(@Validated @RequestBody TemplateSaveParam param){
        //resultId 为templateid >0 表示更新操作成功
        Long resultId = templateServ.save(param);
        return Result.success(resultId);
    }

    @PutMapping("/{id}")
    public Result saveOne(@PathVariable @Min(1) Long id,@Validated @RequestBody TemplateSaveParam param){
        //resultId 为templateid >0 表示更新操作成功
        param.setId(id);
        Long resultId = templateServ.save(param);
        return Result.success(resultId);
    }

    @PutMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable("id") Long id,AvailableStatus status){
        return Result.success(templateServ.changeStatus(id,status));
    }

    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id){
        return Result.success(templateServ.deleted(id));
    }
}
